import Vue from 'vue'
import Router from 'vue-router'
import Tab from '@/pages/Tab'
import Login from '@/pages/Login'
import Room1 from '@/pages/cases/Room1'
import Room2 from '@/pages/cases/Room2'
import Form from '@/pages/my/Form'
import News from '@/pages/my/News'
import Order from '@/pages/my/Order'
import Dingyue from '@/pages/my/Dingyue'
import Mapa from '@/pages/my/Mapa'
Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {// 屏蔽push方法重复点击出现报错
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {// 屏蔽replace方法重复点击出现报错
    return originalReplace.call(this, location).catch(err => err);
};

const router= new Router({
  routes: [
    {
      path:'/',
      redirect:'login'
    },
    {
      path:'/login',
      name:'login',
      component:Login
    },
    {
      path: '/tab',
      name: 'tab',
      component: Tab,
      children:[
        {
          path:'/tab',
          redirect:'home'
        },
        {
          path:'case',
          name:'case',
          component:()=>import('@/pages/tab/Case')
        },
        {
          path:'home',
          name:'home',
          component:()=>import('@/pages/tab/Home')
        },
        {
          path:'sub',
          name:'sub',
          component:()=>import('@/pages/tab/Subscript')
        },
        {
          path:'my',
          name:'my',
          component:()=>import('@/pages/tab/My')
        },
      ]
    },
    {  //点击跳转
      path: '/room1',
      name: 'room1',
      component:Room1
    },
    {  //点击跳转
      path: '/room2',
      name: 'room2',
      component:Room2
    },
    {
      path: '/news',
      name: 'news',
      component: News,
    },
    {
      path: '/dingyue',
      name: 'dingyue',
      component: Dingyue,
    },
    {
      path: '/form',
      name: 'form',
      component: Form,
    },
    {
      path: '/mapa',
      name: 'mapa',
      component: Mapa,
    },
    {
      path: '/order',
      name: 'order',
      component: Order,
    },
  ]
})


// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
// router.beforeEach((to, from, next) => {
//   if (to.path === '/login') {
//     next();
//   } else {
//     let token = localStorage.getItem('Authorization');
 
//     if (token === null || token === '') {
//       next('/login');
//     } else {
//       next();
//     }
//   }
// });
 
export default router